<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Index_Supplier extends Controller_Index_Base
{
    public function action_index()
    {    
        $supplier_id = $this->request->param('id');             
        $supplier    = ORM::factory('supplier', $supplier_id);
        Model_Stat::update_suppliers_views(array($supplier)); 
        
        //$category = ORM::factory('category', $supplier->category_id)

        $content = new View('index/supplier/index_tpl');
        $content->supplier = $supplier;       
        
        $this->print_template($supplier->name, $content);
    }
    
    public function action_goods()
    {    
        $supplier_id = $this->request->param('id');
        $page        = $this->request->param('page') ? $this->request->param('page') : 1;
        $supplier    = ORM::factory('supplier', $supplier_id);
        
        $goods       = ORM::factory('good')->get_goods($supplier->id, $page);
        Model_Stat::update_goods_views($goods); 
        
        $content = new View('index/supplier/goods_tpl');
        $content->supplier = $supplier;
        $content->goods    = $goods;
        
        $content->count    = ORM::factory('good')->get_goods($supplier->id, null, true);
        $content->pagination = array(
            'page'     => $page,
            'per_page' => ORM::factory('good')->get_goods_per_page(),
        );
        
        
        $this->print_template($supplier->name, $content);
    }
    
    public function action_sendcontacts()
    {
        if (HTTP_Request::POST == $this->request->method()) {
            $supplier_id = $this->request->post('supplier_id');
            $good_id     = $this->request->post('good_id');
            $phone       = $this->request->post('phone');
            $email       = $this->request->post('email');
            $text        = $this->request->post('text');
            $date        = date('d.m.Y H:i:s');
            
            $supplier   = ORM::factory('supplier', $supplier_id);
            
            setcookie('phone', $phone, time()+365*24*60*60, '/');
            setcookie('email', $email, time()+365*24*60*60, '/');
            setcookie('text', serialize($text), time()+365*24*60*60, '/');
            
            if ($good_id) {
                Model_Stat::update_good_rank($good_id, 2);
            } else {
                Model_Stat::update_supplier_rank($supplier_id, 2);
            }
            Model_Mailer::lead_notification($supplier, $phone, $email, $text);
            file_put_contents(APPPATH.'../data/contacts.txt', $date."\r\n".$supplier_id."\r\n".$good_id."\r\n".$phone."\r\n".$email."\r\n---\r\n".$text."\r\n---\r\n\r\n", FILE_APPEND);
            
            Request::current()->redirect(Request::current()->referrer());
        }
    }
    
    public function action_update_supplier_rank_ajax()
    {
        $result = array(
            'success'  => false,
        );
        if ($this->request->is_ajax()) {
            $supplier_id = $this->request->post('supplier_id');
            $rank        = $this->request->post('rank');
            
            Model_Stat::update_supplier_rank($supplier_id, $rank);   
            $result = array(
                'success' => true,
            );
        }
        //header('Content-type: text/json');
        echo json_encode($result);
        die();
    }
    
    public function action_update_good_rank_ajax()
    {
        $result = array(
            'success'  => false,
        );
        if ($this->request->is_ajax()) {
            $good_id = $this->request->post('good_id');
            $rank    = $this->request->post('rank');
            
            Model_Stat::update_good_rank($good_id, $rank);   
            $result = array(
                'success' => true,
            );
        }
        //header('Content-type: text/json');
        echo json_encode($result);
        die();
    }
}